package org.pub.app.lunch;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InputAddress extends Activity {

	private final String TAG = "InputAddress";
	private Button ok;
	private Button cancel;
	private EditText inputAddress;
	private Location selectedLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input_address);
		System.out.println("주소");
		inputAddress = (EditText) findViewById(R.id.inputAddr);
		ok = (Button) findViewById(R.id.ok);
		cancel = (Button) findViewById(R.id.cancel);

		ok.setOnClickListener(new View.OnClickListener() {

			// @Override
			public void onClick(View view) {
				// 확인버튼을 누르면 입력한 스트링으로 주소를 검색한다.
				String inputString = inputAddress.getText().toString();
				if (inputString == null || inputString.equals("")) {
					Toast.makeText(view.getContext(), R.string.inputAddress_error, Toast.LENGTH_LONG).show();
				} else {
					Intent in = new Intent(getBaseContext(), RestaurantList.class);
					try {
						Location inputLocation = loadAddress(inputString);
						if (inputLocation != null) {
							in.putExtra("MY CURRENT LOCATION", new Location(inputLocation));
							startActivity(in);
						} else {
							throw new Exception();
						}
					} catch (Exception e) {
						e.printStackTrace();
						Log.w(TAG, e.getMessage());
					}
				}
			}
		});

		cancel.setOnClickListener(new View.OnClickListener() {

			// @Override
			public void onClick(View view) {
				// 취소버튼을 누르면 메인 페이지로 돌아간다.
				Intent init = new Intent(getBaseContext(), LunchSelector.class);
				startActivity(init);
			}
		});
	}

	/**
	 * 해당 주소명에 해당하는 Location 정보를 리턴한다.
	 * 
	 * @param locationName
	 *            주소명
	 * @return Location
	 */
	public Location loadAddress(String locationName) {
		try {
			selectedLocation = new Location(Context.LOCATION_SERVICE);
			Geocoder gc = new Geocoder(this, Locale.KOREA);
			List<Address> locations = gc.getFromLocationName(locationName, 1);
			Address address = null;

			address = locations.get(0);
			//System.out.println("Latitude : " + address.getLatitude() + " Longitude : " + address.getLongitude());
			selectedLocation.setLatitude(address.getLatitude());
			selectedLocation.setLongitude(address.getLongitude());
		} catch (Exception e) {
			new AlertDialog.Builder(this).setTitle(R.string.location_search_title).setMessage(R.string.location_search_message)
					.setNeutralButton(R.string.dialogOKButton, new DialogInterface.OnClickListener() {

						// @Override
						public void onClick(DialogInterface dialog, int which) {
							// 창이 닫힘
							// Intent init = new Intent(getBaseContext(),
							// LunchSelector.class);
							// startActivity(init);
						}
					}).show();

			Log.w(this.TAG, e.getMessage());
			return null;
		}

		return selectedLocation;
	}
}
